local rule = fk.CreateSkill({
  name = "huanhua_rule",
})

Fk:loadTranslationTable{ ["huanhua_rule"] = "幻化之战" }

local SeabornUtil = require "packages/seaborn/utility/utility"

rule:addEffect(fk.GameStart, {
  priority = 0,
  can_refresh = function (self, event, target, player, data)
    return player == target
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    room:setTag("SkipGameRule", true)
    SeabornUtil.createSkillPool(room)
    if not room.settings.enableDeputy then
      room:setBanner("@HuanhuaMode", "#NormalHuanhuaMode")
      for _, p in ipairs(room:getAllPlayers()) do
        room:handleAddLoseSkills(p, "spiritualPower&")
        SeabornUtil.addSiritualPower(p, 2)
      end
      SeabornUtil.doHuanhua(room)
    else
      room:setBanner("@HuanhuaMode", "#BawrolHuanhuaMode")
    end
  end
})

rule:addEffect(fk.RoundStart, {
  priority = 0,
  can_refresh = function (self, event, target, player, data)
    return player == target
    and player.room.settings.enableDeputy
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    SeabornUtil.doHuanhua(room)
  end
})

rule:addEffect(fk.Damage, {
  priority = 0,
  can_refresh = function (self, event, target, player, data)
    return player == target
    and not player.room.settings.enableDeputy
  end,
  on_refresh = function (self, event, target, player, data)
    SeabornUtil.addSiritualPower(player, data.damage)
  end
})

rule:addEffect(fk.GameOverJudge, {
  priority = 0,
  can_refresh = function (self, event, target, player, data)
    return player == target
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    if #room.alive_players == 1 then
      local winner = Fk.game_modes[room.settings.gameMode]:getWinner(player)
      if winner ~= "" then
        room:gameOver(winner)
        return true
      end
    end
  end
})

Fk:loadTranslationTable{
  ["@HuanhuaMode"] = "幻化",
  ["#NormalHuanhuaMode"] = "通常模式",
  ["#BawrolHuanhuaMode"] = "狂暴模式",
}

return rule